package cn.jflow.test;

import bp.da.DBAccess;
import bp.nbport.OrgEmpCRUD;
import bp.wf.Glo;
import bp.wf.Install;
import bp.wf.Upgrader;
import cn.jflow.boot.JFlowApplication;
import org.junit.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;


/**
 * 初始化数据表
 * @author
 * @version 2023-2-28
 */
@ActiveProfiles("test")
@SpringBootTest(classes= JFlowApplication.class)
public class InitData extends AbstractJUnit4SpringContextTests {
    @Test
    public void test1() throws Exception {
        String message=" {\"action\":\"add\",\"appCode\":\"20243\",\"appName\":\"驰骋工作流\",\"category\":\"0\",\"companyName\":null,\"createBy\":null,\"createDate\":null,\"creditCode\":null,\"hrUuid\":\"16564\",\"indexSort\":\"0000001100000030030000002002000000000600000000030000001001\",\"messageId\":\"807dd7314fd442a5b690e4238c49d30f\",\"oldOrgCode\":null,\"orgBasId\":\"16564\",\"orgBusId\":\"16564\",\"orgCode\":\"1003002006003001\",\"orgForm\":\"2\",\"orgFullName\":\"宁波舟山港股份有限公司北仑矿石码头分公司\",\"orgName\":\"安全卫环部\",\"orgType\":\"1\",\"parentCode\":\"16563\",\"parentName\":\"矿石公司本部\",\"shortName\":\"矿石公司\",\"status\":true,\"type\":\"ORG\",\"updateBy\":null,\"updateDate\":null}";
        String message2="{\"action\":\"update\",\"birthday\":null,\"category\":true,\"companyName\":\"宁波舟山港股份有限公司北仑矿石码头分公司\",\"country\":\"中国\",\"createTime\":1715758422325,\"degree\":null,\"education\":\"大学本科\",\"gender\":false,\"idCard\":\"330206198301083121\",\"mail\":\"yulingli@nbport.com.cn\",\"major\":null,\"messageId\":\"5ff012e1a0ab4abba6d3524686ca177f\",\"mobile\":\"15906558783\",\"name\":\"虞玲俐\",\"nationality\":\"汉族\",\"orgCode\":\"16574\",\"partWork\":\"\",\"politicsstatus\":\"中共党员\",\"postName\":\"副经理\",\"shortMobile\":\"688388\",\"signCode\":\"20243\",\"sort\":\"35027\",\"status\":true,\"type\":\"user\",\"userBasId\":\"1246468\",\"userBusId\":\"1303497\",\"userType\":true,\"username\":\"nbg92076\",\"workId\":null,\"workType\":null}";
        String msg3="{\"action\":\"add\",\"birthday\":null,\"category\":true,\"companyName\":\"宁波油港轮驳有限公司\",\"country\":\"中国\",\"createTime\":1716343419907,\"degree\":null,\"education\":\"普通高中\",\"gender\":true,\"idCard\":\"330204197504080011\",\"mail\":\"\",\"major\":null,\"messageId\":\"6d48372c614544178cad645d96f98f89\",\"mobile\":\"13567876560\",\"name\":\"毛奉甬\",\"nationality\":\"汉族\",\"orgCode\":\"16612\",\"partWork\":null,\"politicsstatus\":\"群众\",\"postName\":\"门卫\",\"shortMobile\":\"636560\",\"signCode\":\"20243\",\"sort\":\"55640\",\"status\":true,\"type\":\"user\",\"userBasId\":\"1250763\",\"userBusId\":\"1307792\",\"userType\":true,\"username\":\"NBG24504\",\"workId\":null,\"workType\":null}";
        OrgEmpCRUD orgEmpCRUD = new OrgEmpCRUD();
        orgEmpCRUD.dealOrgEmp(msg3);
    }

    @Test
    public void initData() throws Exception{
        bp.da.Log.DebugWriteInfo("数据库初始化开始。");
        if(CheckIsDBInstall()){
            Install.IsCanInstall();
        }
        //运行ccflow的安装.
        Install.DoInstallDataBase("CH", 2);
        //执行ccflow的升级。
        Upgrader.UpdataCCFlowVer();
        //加注释.
        bp.pub.PubClass.AddComment();

        if (DBAccess.IsExitsTableCol("Port_Emp", "EmpSta") == true)
            DBAccess.DropTableColumn("Port_Emp", "EmpSta");

        if (DBAccess.IsExitsObject("WF_Flow") == true)
            bp.da.Log.DebugWriteInfo("数据库初始化完成。");
    }



    /**
     * 是否需要数据库连接
     * @return
     * @throws Exception
     */
    private boolean CheckIsDBInstall() throws Exception {
        //检查数据库连接.
        try
        {
            DBAccess.TestIsConnection();
        }
        catch (Exception ex)
        {
            throw new Exception("err@异常信息:" + ex.getMessage());
        }

        //检查是否缺少Port_Emp 表，如果没有就是没有安装.
        if (DBAccess.IsExitsObject("Port_Emp") == false && DBAccess.IsExitsObject("WF_Flow") == false)
            return true;

        //如果没有流程表，就执行安装.
        if (DBAccess.IsExitsObject("WF_Flow") == false)
            return true;
        return false;
    }


}
